﻿namespace Vdc.Interface
{
    using Vdc.Interface.Design;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [Designer(typeof(VdcImageButtonDesigner), typeof(IDesigner)), PersistChildren(false), ParseChildren(true)]
    public class VdcImageButton : ImageButton
    {
        private static Hashtable availableClientIds = new Hashtable();
        private string clientObjectID;
        private bool IsFocused;
        private VdcSpan mainContainer;
        internal ScriptManagerHelper MyScriptManager;
        private Literal scriptContainerContent;

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(this.ID))
            {
                base.Attributes["id"] = this.ClientID;
            }
            this.mainContainer = new VdcSpan();
            this.mainContainer.ToolTip = this.ToolTip;
            VdcDiv child = new VdcDiv();
            child.Style["display"] = "none";
            child.CssClass = "ob_iCallbackScript";
            this.scriptContainerContent = new Literal();
            child.Controls.Add(this.scriptContainerContent);
            this.mainContainer.Controls.Add(child);
            base.CreateChildControls();
        }

        private string CreateClientScript(bool createOnlyClientObject)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" if(typeof(" + this.ClientObjectID + ")!='undefined'){if(typeof(" + this.ClientObjectID + ".destruct)=='function'){" + this.ClientObjectID + ".destruct();}}");
            if (!createOnlyClientObject)
            {
                builder.Append("var " + this.ClientObjectID + ";");
                builder.Append("function initImageButton__" + this.ClientObjectID + "(attempts){ ");
                builder.Append("if(typeof(vdcImageButtonJSLoaded) == \"undefined\" || typeof(Vdc.Interface.VdcImageButton) == \"undefined\") {if(attempts<30){window.setTimeout(\"initImageButton__" + this.ClientObjectID + "(\" + (attempts+1) + \")\", 500);}else{alert(\"There was a problem referencing ImageButton javascript files.  Please contact vdccontrol@gmail.com for support.\");}return;}");
                builder.Append("try{");
            }
            builder.Append(this.ClientObjectID + " = new Vdc.Interface.VdcImageButton(");
            builder.Append("\"" + this.ClientObjectID + "\",");
            builder.Append("\"" + this.ClientID + "\",");
            builder.Append(this.Enabled.ToString().ToLower() + ",");
            builder.Append(this.IsFocused.ToString().ToLower() + ",");
            builder.Append("{");
            builder.Append("\"N\":\"" + this.ImageUrl + "\",");
            builder.Append("\"O\":\"" + this.ImageUrl_Over + "\",");
            builder.Append("\"F\":\"" + this.ImageUrl_Focused + "\",");
            builder.Append("\"P\":\"" + this.ImageUrl_Pressed + "\",");
            builder.Append("\"D\":\"" + this.ImageUrl_Disabled + "\"");
            builder.Append("},");
            builder.Append(createOnlyClientObject.ToString().ToLower());
            builder.Append(");");
            if (!createOnlyClientObject)
            {
                builder.Append("}catch(e){alert(\"There was an error initializing ImageButton with ID " + this.ClientObjectID + ".\\n\\n\" + e.message + \"\\n\\nPlease contact vdccontrol@gmail.com for help.\");}}");
                builder.Append(" initImageButton__" + this.ClientObjectID + "(0); ");
            }
            return builder.ToString();
        }

        public override void Focus()
        {
            this.IsFocused = true;
            base.Focus();
        }

        protected override void OnInit(EventArgs e)
        {
            this.MyScriptManager = new ScriptManagerHelper(this);
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            VdcControlFactory.RegisterClientScripts(this, typeof(VdcImageButton), this.MyScriptManager);
            base.OnPreRender(e);
        }

        public static void RegisterClientScripts(Control control)
        {
            ScriptManagerHelper scriptManager = new ScriptManagerHelper(control);
            VdcControlFactory.RegisterClientScripts(control, typeof(VdcImageButton), scriptManager);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Page != null)
            {
                this.Page.VerifyRenderingInServerForm(this);
            }
            this.EnsureChildControls();
            if (VdcControlFactory.IsDesignTimeRender(this) && !base.ChildControlsCreated)
            {
                this.CreateChildControls();
            }
            VdcHtmlTextWriter writer2 = new VdcHtmlTextWriter(writer.InnerWriter, "");
            bool flag = true;
            if (VdcControlFactory.IsInsideGridWithCallback(this))
            {
                flag = false;
                this.scriptContainerContent.Text = this.CreateClientScript(true);
            }
            base.Render(writer2);
            this.mainContainer.RenderControl(writer2);
            if (flag && !VdcControlFactory.IsDesignTimeRender(this))
            {
                if (!VdcControlFactory.CheckAjaxPageInheritance(this))
                {
                    this.MyScriptManager.RegisterStartupScript(this.ClientID, this.CreateClientScript(false), true);
                }
                else
                {
                    writer2.Write("<script type=\"text/javascript\">" + this.CreateClientScript(false) + "</script>");
                }
            }
        }

        protected string ClientObjectID
        {
            get
            {
                if (string.IsNullOrEmpty(this.clientObjectID))
                {
                    this.clientObjectID = VdcControlFactory.GetClientObjectID(this, availableClientIds);
                }
                return this.clientObjectID;
            }
            set
            {
                this.clientObjectID = value;
            }
        }

        [Bindable(true), Description("The url to the image file used for the 'disabled' state.")]
        public string ImageUrl_Disabled
        {
            get
            {
                object obj2 = this.ViewState["ImageUrl_Disabled"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["ImageUrl_Disabled"] = value;
            }
        }

        [Bindable(true), Description("The url to the image file used for the 'focused' state.")]
        public string ImageUrl_Focused
        {
            get
            {
                object obj2 = this.ViewState["ImageUrl_Focused"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["ImageUrl_Focused"] = value;
            }
        }

        [Description("The url to the image file used for the 'over' state."), Bindable(true)]
        public string ImageUrl_Over
        {
            get
            {
                object obj2 = this.ViewState["ImageUrl_Over"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["ImageUrl_Over"] = value;
            }
        }

        [Description("The url to the image file used for the 'pressed' state."), Bindable(true)]
        public string ImageUrl_Pressed
        {
            get
            {
                object obj2 = this.ViewState["ImageUrl_Pressed"];
                if (obj2 != null)
                {
                    return obj2.ToString();
                }
                return "";
            }
            set
            {
                this.ViewState["ImageUrl_Pressed"] = value;
            }
        }
    }
}

